package com.bbs.controller;

import com.bbs.pojo.Comment;
import com.bbs.properties.MyUserDetails;
import com.bbs.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @PostMapping("/add")
    public String addComment(Comment comment, Authentication authentication) {
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        Long id = userDetails.getId(); // 安全获取 id
        // 设置当前用户的ID，假设当前用户ID为1，实际项目中应从会话中获取
        comment.setUserId(Math.toIntExact(id));
        commentService.save(comment);
        return "redirect:/user/index/detail/" + comment.getContextId();
    }
}